package com.sky.controller.admin;

import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.SetMealService;
import com.sky.service.impl.SetMealServiceImpl;
import com.sky.vo.SetmealVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("/admin/setmeal")
@RestController
@Slf4j
@Api(tags = "套餐接口")
public class SetMealController {
    @Autowired
    private SetMealServiceImpl setMealService;

    @GetMapping("/page")
    @ApiOperation("套餐分页查询")
    public Result<PageResult> pageSelect(SetmealPageQueryDTO setmealPageQueryDTO) {
        log.info("{}", setmealPageQueryDTO);
        PageResult page = setMealService.page(setmealPageQueryDTO);
        return Result.success(page);
    }

    @PostMapping
    @ApiOperation("新增套餐")
    @CacheEvict(cacheNames = "setMealCache",key="#setmealDTO.categoryId")
    public Result addSetmeal(@RequestBody SetmealDTO setmealDTO) {
        log.info("{}", setmealDTO);
        setMealService.add(setmealDTO);
        return Result.success();

    }

    @ApiOperation("根据id返回套餐信息")
    @GetMapping("/{id}")
    public Result<SetmealVO> selectById(@PathVariable Integer id) {
        SetmealVO select = setMealService.select(id);
        return Result.success(select);
    }

    @DeleteMapping
    @ApiOperation("套餐批量删除")
    @CacheEvict(cacheNames = "setMealCache",allEntries = true)
    public Result deleteByIds(String ids) {
        setMealService.deleteByIds(ids);
        return Result.success();
    }
    @PutMapping
    @ApiOperation("套餐修改")
    @CacheEvict(cacheNames = "setMealCache",allEntries = true)
    public Result updateSetmeal(@RequestBody SetmealDTO setmealDTO) {
        setMealService.update(setmealDTO);
        return Result.success();
    }

    @PostMapping("/status/{status}")
    @CacheEvict(cacheNames = "setMealCache",allEntries = true)
    public Result setStatus(@PathVariable Integer status, Integer id) {
        setMealService.status(status,id);
        return Result.success();
    }
}
